<?php

namespace app\api\controller;

use think\View;
use think\Controller;
use think\Db;
use app\data\Data;
use app\api\model\Common;
use \tp5er\Backup;
use think\Session;
use think\Request;

class Param extends Index
{

    public function index()
    {
        $name = trim(input('get.name'));
        $zgh_param = new data($name);
        $res = $zgh_param->_find();
        if ($name == 'keyword') {
            $res = json_decode($res['keyword'], true);
            // $res = explode(',', $res);
        }
        if (isset($res) || empty($res)) {
            $this->_jsonp('200', '数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '数据获取失败', null);
        }
    }

    public function edit()
    {
        if (!isset($_GET['status'])) $this->_jsonp('1001', '参数传输失败', null);
        $status = input('get.status');
        if ($status == 1) {
            $data['name'] = input('get.name');
            $data['address'] = input('get.address');
            $data['link'] = input('get.link');
            $data['root'] = input('get.root');
            $data['info'] = input('get.info');
            $data['keyword'] = input('get.keyword');
            $data['w_r_n'] = input('get.w_r_n');
            $data['remark'] = input('get.remark');
            $table = 'index';
        } else if ($status == 2) {
            $textarea = input('get.textarea');
            $data['keyword'] = json_encode($textarea);
            $table = 'keyword';
        } else {
            $data['i_address'] = input('get.i_address');
            $data['i_wide'] = input('get.i_wide');
            $data['i_high'] = input('get.i_high');
            $data['i_link'] = input('get.i_link');
            $table = 'img';
        }

        $zgh_table = new data($table);
        $where['id'] = 1;
        $res = $zgh_table->_update($data, $where);
        if ($res !== false) {
            action_log('参数管理新增', 'log_admin',1);
            $this->_jsonp('200', '添加成功', null);
        } else {
            action_log('参数管理新增', 'log_admin');
            $this->_jsonp('1000', '添加失败', null);
        }
    }

    public function backup()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();

        $where['name'] = array('like', "%$username%");
        $where['time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');

        $zgh_backup = new Data('backup');
        $res['data'] = $zgh_backup->selectPage($page, $where);
        $res['count'] = $zgh_backup->count($where);
        if (isset($res) || empty($res)) {
            $this->_jsonp('200', '数据库备份信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '数据库备份信息查询失败', null);
        }
    }

	
}